﻿using UnityEngine;
using System.Collections;
using PBMessage;
public class PacketHelper
{
    public void SendUserLogin(string userName)
    {
        CS_Login login = new CS_Login();
        login.UserKey = userName;
        login.MachineID = SystemInfo.deviceUniqueIdentifier;
        login.ServerId = 0;
        Game.Net.SendPacket<CS_Login>(login, PBMessageID.CS_Login);
    }

    public void SendMatchGame() 
    {
        PlayerModel playerModel = Game.Model.NewGet<PlayerModel>();
        CS_MatchGame matchGame = new CS_MatchGame();
        matchGame.UserName = playerModel.UserName;
        Game.Net.SendPacket<CS_MatchGame>(matchGame,PBMessageID.CS_MatchGame);
    }

    public void SendCancelMatch() 
    {
        PlayerModel playerModel = Game.Model.NewGet<PlayerModel>();
        CS_CancelMatch cancelMatchGame = new CS_CancelMatch();
        cancelMatchGame.UserName = playerModel.UserName;
        Game.Net.SendPacket<CS_CancelMatch>(cancelMatchGame, PBMessageID.CS_CancelMatch);
    }

    public void SendSynReadyPlayerState(bool isReady,string selHeroID) 
    {
        ReadyRoomModel model = Game.Model.Get<ReadyRoomModel>();
        CS_SynRoomPlayerState synPlayerState = new CS_SynRoomPlayerState();
        synPlayerState.IsReady = true;
        synPlayerState.RoomID  = model.RoomID;
        synPlayerState.SelectId = selHeroID;
        synPlayerState.Camp = model.Camp;
        synPlayerState.UserName = Game.Model.Get<PlayerModel>().UserName;
        Game.Net.SendPacket<CS_SynRoomPlayerState>(synPlayerState,PBMessageID.CS_SynRoomPlayerState);
    }

    public void SendMoveToTarget(string unitID,float tx,float ty,float tz) 
    {
        CS_AbilityMove move= new CS_AbilityMove();
        move.TargetPosX = tx;
        move.TargetPosY = ty;
        move.TargetPosZ = tz;
        move.UnitID = unitID;

        Game.Net.SendPacket<CS_AbilityMove>(move,PBMessageID.CS_AbilityMove);
    }

    public enum PushCmdUnitType
    {
       Push,
       ClearPush 
    }
    public void SendPushUnitCommand(string UnitId, CommandType cmdType, PushCmdUnitType pushType, object[] args)
    {
        CS_PushUnitCommand pushcmd = new CS_PushUnitCommand();
        pushcmd.UnitID = UnitId;
        pushcmd.CmdId = (int)cmdType;
        pushcmd.PushType = (int)pushType;

        if (args != null)
        {
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].GetType() == typeof(int)) 
                {
                    pushcmd.Ints.Add((int)args[i]);
                }
                if (args[i].GetType() == typeof(float))
                {
                    pushcmd.floats.Add((float)args[i]);
                }
                if (args[i].GetType() == typeof(string))
                {
                    pushcmd.strs.Add((string)args[i]);
                }
            }
        }

        Game.Net.SendPacket<CS_PushUnitCommand>(pushcmd,PBMessageID.CS_PushUnitCommand);
    }

    public void SendAttack(string unitID) 
    {
        CS_AbilityAttack attack = new CS_AbilityAttack();
        attack.UnitID = unitID;
        Game.Net.SendPacket<CS_AbilityAttack>(attack,PBMessageID.CS_AbilityAttack);
    }
}
